package fragments;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

import network.NetworkHandler;
import stockrobot.android.R;
import util.FinancialLongConverter;
import android.settingsActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PortfolioFragment  extends Fragment implements PropertyChangeListener {
	private TextView tvPortValue;
    /** Called when the activity is first created. */
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }
	 	  setHasOptionsMenu(true);
		return (LinearLayout)inflater.inflate(R.layout.portfolio, container, false);
	}  
	
 	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
 		inflater.inflate(R.menu.connected_menu, menu);
 		super.onCreateOptionsMenu(menu, inflater);
 	}
    
    @Override
    public void onActivityCreated(Bundle bundle) {
    	super.onActivityCreated(bundle);
	    tvPortValue = (TextView) getActivity().findViewById(R.id.tvPortfolioValue);
    	final NetworkHandler network = NetworkHandler.instance();
    	if(network.isFakeServer()){
	    	tvPortValue.setText("11871,64");
	    } else {
			network.addObserver(this);
			Handler refresh = new Handler(Looper.getMainLooper()); 
			refresh.post(new Runnable() { 
			    public void run() 
			    { 
			    	updatePortfolioValue();
			    } 
			});
	    }

    }
 	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// Handle item selection    
    	switch (item.getItemId()) {
    		case R.id.disconnect: 
    			disconnect();
    			return true;
    		case R.id.about: 
    			showAbout();
    			return true;
    		case R.id.settings: 
    			showSettings();
    			return true;
    		default:
    			return super.onOptionsItemSelected(item);    
    		}
    	}

    private void showSettings() { 
	    Intent prefIntent = new Intent(getActivity(),settingsActivity.class);   
	    this.startActivity(prefIntent); 
	}
    private void showAbout() {
		Toast.makeText(getActivity(), "ASTRo Stock-Robot android by Erik Ramqvist", Toast.LENGTH_SHORT).show();
	}
    
 	private void disconnect() {
		  Toast.makeText(getActivity(), "Disconnected.", Toast.LENGTH_SHORT).show();
		  getActivity().finish();
	}
    
 	private void updatePortfolioValue() {
		tvPortValue.setText(FinancialLongConverter.toStringTwoDecimalPoints(NetworkHandler.instance().getPortFolioValue()));
	}


	public void propertyChange(PropertyChangeEvent event) {
		if(event.getPropertyName().equals("PortfolioValueChanged")){
			Handler refresh = new Handler(Looper.getMainLooper()); 
			refresh.post(new Runnable() { 
			    public void run() 
			    { 
			        updatePortfolioValue(); 
			    } 
			}); 
		}
	} 
}
